package com.fly.show.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fly.show.service.GitService;

@Controller
public class IndexController
{
    @Autowired
    GitService gitService;
    
    @GetMapping("/")
    public String index(String name, Model model)
    {
        if (StringUtils.isBlank(name))
        {
            model.addAttribute("names", gitService.getSubNames());
        }
        else
        {
            name = StringUtils.trimToEmpty(name);
            model.addAttribute("name", name);
            model.addAttribute("names", gitService.search(name));
        }
        return "index";
    }
    
    @PostMapping("/")
    public String index(String name, RedirectAttributes redirectAttributes)
    {
        redirectAttributes.addAttribute("name", name);
        return "redirect:/";
    }
}
